package com.travel.server.manager.impl.dal.data;

import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.impl.dal.MemberGradeDal;
import com.travel.server.manager.model.MemberGradeInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/10 18:11
 * @Desc:
 */
@Component
public class CustomerDataDal {

    @Autowired
    private MemberGradeDal memberGradeDal;

    public final static String MEMBER = "会员";

    public final static String HIGH_MEMBER = "优质会员";

    public MemberGradeInfo getHighMember(){
        MemberGradeInfo member = select(HIGH_MEMBER);
        if(member == null){
            WorkCode workCode = WorkCode.MEMBER_NOT_EXIST;
            workCode.setMessage("优质会员等级未初始化");
            throw new WorkException(workCode);
        }
        return member;
    }

    public MemberGradeInfo getMember(){
        MemberGradeInfo member = select(MEMBER);
        if(member == null){
            WorkCode workCode = WorkCode.MEMBER_NOT_EXIST;
            workCode.setMessage("会员等级未初始化");
            throw new WorkException(workCode);
        }
        return member;
    }

    private MemberGradeInfo select(String name){
        MemberGradeDal.MemberGradeParam param = new MemberGradeDal.MemberGradeParam();
        param.setName(name);
        List<MemberGradeInfo> list = memberGradeDal.selectByExample(param);
        return CollectionUtil.getFirst(list);
    }
}
